package com.mf.morefun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.mf.morefun.R;
import com.mf.morefun.bean.DoHomeWorkListBean;
import com.mf.morefun.listener.MyRequestListener;
import com.mf.morefun.util.TransformationUtils;
import com.mf.morefun.widget.GlideRoundTransform;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CreateClassHomeWorkAdapter extends BaseAdapter {
    private Context mContext;
    private List<DoHomeWorkListBean> mHomeWorkList;
    private int selectPosition = 0;

    public int getSelectPosition() {
        return selectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public CreateClassHomeWorkAdapter(Context mContext, List<DoHomeWorkListBean> mHomeWorkList) {
        this.mContext = mContext;
        this.mHomeWorkList = mHomeWorkList;
    }

    @Override
    public int getCount() {
        return mHomeWorkList.size();
    }

    @Override
    public Object getItem(int position) {
        return mHomeWorkList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (null == convertView) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_classhomeworkitem, null);
        } else {
            view = convertView;
        }
        if (null != mHomeWorkList && mHomeWorkList.size() > 0) {
            ViewHolder holder = new ViewHolder(view);
            holder.tvBookname.setText(mHomeWorkList.get(position).getMfTpLRR().getName());
            if (mHomeWorkList.get(position).getMfTpLRR().getType().equals("3")) {
                holder.tvType.setText("录音");
                RequestOptions options = new RequestOptions().transform(new GlideRoundTransform(mContext, 16));
                Glide.with(mContext).asBitmap().load(mHomeWorkList.get(position).getMfTpLRR().getBook().getCover()).listener(new MyRequestListener(holder.ivBookimg, mContext, (int) ((DeviceUtils.deviceWidth(mContext) / 3) - 60), (DeviceUtils.deviceHeight(mContext) / 2 - 120))).apply(options).into(new TransformationUtils(holder.ivBookimg, mContext, (int) ((DeviceUtils.deviceWidth(mContext) / 3) - 60), (DeviceUtils.deviceHeight(mContext) / 2 - 120)));
            } else if (mHomeWorkList.get(position).getMfTpLRR().getType().equals("4")) {
                holder.tvType.setText("视频");
                RequestOptions options = new RequestOptions().transform(new GlideRoundTransform(mContext, 16));
                Glide.with(mContext).asBitmap().load(mHomeWorkList.get(position).getMfTpLRR().getBook().getCover()).listener(new MyRequestListener(holder.ivBookimg, mContext, (int) ((DeviceUtils.deviceWidth(mContext) / 3) - 60), (DeviceUtils.deviceHeight(mContext) / 2 - 120))).apply(options).into(new TransformationUtils(holder.ivBookimg, mContext, (int) ((DeviceUtils.deviceWidth(mContext) / 3) - 60), (DeviceUtils.deviceHeight(mContext) / 2 - 120)));
            } else {
                holder.tvType.setText("创作");
                RequestOptions options = new RequestOptions().transform(new GlideRoundTransform(mContext, 16));
                Glide.with(mContext).asBitmap().load(mHomeWorkList.get(position).getMfTpCreation().getPicDescription()).listener(new MyRequestListener(holder.ivBookimg, mContext, (int) ((DeviceUtils.deviceWidth(mContext) / 3) - 60), (DeviceUtils.deviceHeight(mContext) / 2 - 120))).apply(options).into(new TransformationUtils(holder.ivBookimg, mContext, (int) ((DeviceUtils.deviceWidth(mContext) / 3) - 60), (DeviceUtils.deviceHeight(mContext) / 2 - 120)));

            }
            if (getSelectPosition() == position){
                holder.llBg.setBackgroundResource(R.drawable.line_blue);
            }else {
                holder.llBg.setBackgroundResource(R.drawable.gift_shapes);
            }
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.iv_bookimg)
        ImageView ivBookimg;
        @BindView(R.id.tv_bookname)
        TextView tvBookname;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.ll_bg)
        LinearLayout llBg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
